package pers.vinsonws.jianghu.clientwin.ui;

import pers.vinsonws.jianghu.clientwin.global.Info;
import pers.vinsonws.jianghu.clientwin.model.ChatRoomClient;
import pers.vinsonws.jianghu.clientwin.service.InitChatService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;

/**
 * @auther vinsonws
 * @date 2018/11/12 13:19
 */
public class IndexUi extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel mainPanel;
    private JPanel chatRoomList;
    private HashMap<Integer, JButton> jButtonHashMap;

    private final JTabbedPane tabbedPane;
    private final int startX = 150;
    private final int startY = 100;

    public IndexUi() {
        addWindowListener(new WindowAdapter() {


            public void windowClosing(WindowEvent e) {

                Info.closeChannel();
            }

        });
        UIManager.put("TabbedPane.contentOpaque", false);
        tabbedPane = new JTabbedPane();
        Info.tabbedPane = tabbedPane;
        //设置框架
        jButtonHashMap = new HashMap<>();
        setTitle("JH-江湖");
        setSize(1176, 664);
        //setLocation(220, 120);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setUndecorated(false);                //

        mainPanel = new JPanel();
        mainPanel.setOpaque(false);
        mainPanel.setLayout(null);
        mainPanel.setBackground(Color.WHITE);
        setContentPane(mainPanel);
        //用户信息



        //聊天室列表
        chatRoomList = new JPanel();
        chatRoomList.setBackground(Color.white);
        tabbedPane.addTab("主页", chatRoomList);
        //tabbedPane.setOpaque(false);



        setLayout(new BorderLayout());
        add(tabbedPane, BorderLayout.CENTER);
        mainPanel.add(tabbedPane);
        Info.onlineUserUi = new OnlineUserUi();

    }


    public void paintChatRoom(){
        jButtonHashMap.clear();
//        for (int i=0;i<chatRoomList.getComponentCount();i++){
//            chatRoomList.remove(i);
//
//        }
        chatRoomList.removeAll();
        for (ChatRoomClient chatRoomClient: Info.chatRoomClientHashMap.values()){
            createChatRoomButton(chatRoomClient.getId(),
                    chatRoomClient.getName(), chatRoomClient.isBelongToMe());
        }
        chatRoomList.revalidate();
    }
    private void createChatRoomButton(Integer chatRoomId, String chatRoomName, boolean belongToMe){
//        for (int id:jButtonHashMap.keySet()){
//            if (id == chatRoomId){
//                chatRoomList.remove(jButtonHashMap.get(id));
//                jButtonHashMap.remove(id);
//            }
//        }
        JButton createChatRoom = new JButton(chatRoomName);
        Info.initUserStatus = false;
        createChatRoom.setFont(new Font("宋体", Font.CENTER_BASELINE,32));
        int size ;
        if (jButtonHashMap != null) size = jButtonHashMap.size();
        else size = 0;
//        createChatRoom.setBounds(startX + (size % 5) * 200, startY + (size / 5 ) * 200,
//                200, 200);
        createChatRoom.setBounds(jButtonHashMap.size() + (size % 5) * 200, jButtonHashMap.size() + (size / 5 ) * 200,
        200, 200);
        createChatRoom.setContentAreaFilled(true);
        if (belongToMe){
            createChatRoom.setBackground(Color.cyan);
            createChatRoom.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (Info.chatRoomOpened.get(chatRoomId) == null){
                        ChatRoomUi chatRoom = new ChatRoomUi();
                        chatRoom.openChatRoomWin(chatRoomId, chatRoomName);
                        Info.chatRoomOpened.put(chatRoomId, chatRoom);
                    }
                }
            });
        } else {
            createChatRoom.setBackground(Color.lightGray);
            createChatRoom.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    new NotJoinChatRoomUi(chatRoomId, chatRoomName);
                }
            });
        }

        chatRoomList.add(createChatRoom);

        jButtonHashMap.put(chatRoomId, createChatRoom);
    }

    public JTabbedPane getTabbedPane() {
        return tabbedPane;
    }
}
